package com.liezh.publisherobserver;

import java.util.HashSet;
import java.util.Set;

/**
 * @author liezh
 * @date 2021-10-29
 */
public class MessagePublisher implements Subject {
    private Set<MsgObserver> msgObservers = new HashSet<>();

    @Override
    public void attach(MsgObserver observer) {
        msgObservers.add(observer);
    }

    @Override
    public void detach(MsgObserver observer) {
        msgObservers.remove(observer);
    }

    @Override
    public void notify(Msg msg) {
        msgObservers.forEach(observer -> observer.update(msg));
    }
}
